﻿using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;
using Portfolio.DataModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.Entity.Migrations;

namespace Portfolio.Data
{
    public class Seeder
    {
        public static void Seed(ApplicationDbContext db, bool seedUsers = true, bool seedProjects = true, bool seedProjectImage = true)
        {
            List<ApplicationUser> UserList = new List<ApplicationUser>();

            var admin = new ApplicationUser()
            {
                FirstName = "Joshua",
                LastName = "Yi",
                UserName = "Admin",
                Email = "gojoshyi@gmail.com",
            };

            UserList.Add(admin);

            if (seedUsers)
            {
                var roleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(db));
                roleManager.Create(new IdentityRole { Name = "Admin" });
                UserManager<ApplicationUser> userMgr = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(db));

                foreach (var user in UserList)
                {
                    if (userMgr.FindByName(user.UserName) == null)
                    {
                        var result = userMgr.Create(user, "gojoshyi");
                    }
                }
                db.SaveChanges();
                var au = db.Users.Where(x => x.UserName == "Admin").FirstOrDefault();
                userMgr.AddToRole(au.Id, "Admin");
            }
            if (seedProjects)
            {
                db.Projects.AddOrUpdate(
                    x => x.Title,

                    new Project { LogoUrl = "https://box1009.bluehost.com:2083/cpsess1781577645/viewer/home3%2fcarlifea%2fpics/B-techBidsLogov2.png", Title = "BtechBids", WindowsAzureUrl = "joshyibtechbids.azurewebsites.net", CodePlexUrl = "https://btechbids.codeplex.com/", Description = "btechbids" },
                    new Project { LogoUrl = "https://box1009.bluehost.com:2083/cpsess1781577645/viewer/home3%2fcarlifea%2fpics/gameoasis_logo_360v2.png", Title = "GameOasis", WindowsAzureUrl = "gameoasis.azurewebsites.net", CodePlexUrl = "https://projectgamer.codeplex.com/", Description = "project gamer: game oasis" }
                    );
                db.SaveChanges();
            }
            if (seedProjectImage)
            {
                db.ProjectImages.AddOrUpdate(
                    x => x.Url,
                    new ProjectImage { Url = "First Image - btech", ProjectId = 1 },
                    new ProjectImage { Url = "Second Image - btech", ProjectId = 1 },
                    new ProjectImage { Url = "Third Image - btech", ProjectId = 1 },
                    new ProjectImage { Url = "First Image - gameoasis", ProjectId = 2 },
                    new ProjectImage { Url = "Second Image - gameoasis", ProjectId = 2 },
                    new ProjectImage { Url = "Third Image - gameoasis", ProjectId = 2 }
                    );
                db.SaveChanges();
            }
        }
    }
}
